package ddemo.viewAndcontroller;

import ddemo.pojo.*;
import ddemo.service.*;
import ddemo.util.numberUtil;

import java.util.*;

public class manageView {

    public static         Set<Integer> chargerids=new HashSet<>();
    public static         Set<Integer> interfaceids=new HashSet<>();
    public static         Set<Integer> parkinglots=new HashSet<>();
    public static         Set<Integer> parkingspots=new HashSet<>();
    public static void initialize() {
        List<Charger> Chargers;
        Chargers=ChargerService.getCharger();
        for (Charger c : Chargers) {
            chargerids.add(c.getId());
        }
        List<Interface> Interfaces;
        Interfaces=InterfaceService.getInterfaces();
        for (Interface i : Interfaces){
            interfaceids.add(i.getId());
        }
        List<Parkinglot> Parkinglots;
        Parkinglots= ParkinglotService.getCharger();
        for (Parkinglot c : Parkinglots) {
            parkinglots.add(c.getId());
        }
        List<Parkingspot> Parkingspots;
        Parkingspots=ParkingspotService.getParkingspots();
        for (Parkingspot i : Parkingspots){
            parkingspots.add(i.getId());
        }

    }
    static {
        initialize();
    }
    public static void chargerManage(){
        while (true) {
            System.out.println("选择您要进行的操作");
            System.out.println("1.添加充电桩");
            System.out.println("2.添加充电口");
            System.out.println("3.删除充电口");
            System.out.println("4.修改充电桩的接口数和位置");
            System.out.println("5.退出程序");
            String num = new Scanner(System.in).next();
            switch (num) {
                case "1":
                    addCharger();
                    break;
                case "2":
                    addInterface();
                    break;
                case "3":
                    deleteInterface();
                    break;
                case "4":
                    updateCharger();
                    break;
                case "5":
                    return;
                default:
                    break;
            }
        }

    }

    public static void parkManage() {
        while (true) {
            System.out.println("选择您要进行的操作");
            System.out.println("1.添加停车场");
            System.out.println("2.添加停车位");
            System.out.println("3.删除停车位");
            System.out.println("4.修改停车场的车位数和位置");
            System.out.println("5.退出程序");
            String num = new Scanner(System.in).next();
            switch (num) {
                case "1":
                    addParkinglot();
                    break;
                case "2":
                    addParkingspot();
                    break;
                case "3":
                    deleteParkingspot();
                    break;
                case "4":
                    updateParkinglot();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("错误输入，请重试");
                    continue;
            }
        }

    }


    public static void shangpai() {
        List<Ask> askid=new ArrayList<>();
        Set<Integer> ids=new HashSet<Integer>();
        askid= AskService.getAskid();
        for(Ask aksid : askid){
            ids.add(aksid.getAskid());
            System.out.println("已发出申请的id:"+aksid.getAskid()+"姓名:"+aksid.getAsk_username()+"手机号"+aksid.getAsk_phone_number());
        }
        System.out.println("选择您要同意的上牌申请");
        String id=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(id)||!ids.contains(Integer.parseInt(id))){
            System.out.println("输入错误请重试");
            return;
        }
        AskService.shangpai(Integer.parseInt(id));
        System.out.println("上牌成功,3s后跳回");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("————————————————");
        System.out.println("————————————————");
    }
    private static void updateCharger() {
        initialize();
        System.out.println("请输入您要修改的充电桩id");
        String id=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(id)||!chargerids.contains(Integer.parseInt(id))){
            System.out.println("输入错误的字符或该充电口不存在，请重试");
            return;
        }
        System.out.println("请输入您想要修改的充电桩的接口数和位置");
        String chargerNumber=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(chargerNumber)){
            System.out.println("请输入正确的数字");
            return;
        }
        String position=new Scanner(System.in).next();
        ChargerService.updateCharger(Integer.parseInt(chargerNumber),position,Integer.parseInt(id));
    }


    private static void deleteInterface() {
        System.out.println("输入您要删除的充电口ID");
        String id=new Scanner(System.in).next();
        List<Interface> Interfaces;
        initialize();
        if(!numberUtil.isNumeric(id)||!interfaceids.contains(Integer.parseInt(id))){
            System.out.println("输入错误的字符或该充电口不存在，请重试");
            return;
        }
        InterfaceService.deleteInterface(Integer.parseInt(id));
        System.out.println("删除成功");
    }

    private static void addInterface() {
        System.out.println("请输入您要添加的充电口的所属充电站id");
        initialize();
        String id=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(id)||!chargerids.contains(Integer.parseInt(id))){
            System.out.println("输入错误的字符或该充电站不存在，请重试");
            return;
        }
        InterfaceService.addInterface(Integer.parseInt(id));
        System.out.println("添加成功");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private static void addCharger() {
        String chargerNumber;
        String position;
        System.out.println("请输入您要添加的充电站的充电口数");
        chargerNumber=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(chargerNumber)){
            return;
        }
        System.out.println("请输入您要添加的充电站的位置");
        position = new Scanner(System.in).next();

        ChargerService.addCharger(Integer.parseInt(chargerNumber),position);

    }

    private static void deleteParkingspot() {
        initialize();
        System.out.println("输入您要删除的停车位ID");
        String id=new Scanner(System.in).next();
        initialize();
        if(!numberUtil.isNumeric(id)||!parkingspots.contains(Integer.parseInt(id))){
            System.out.println("输入错误的字符或该停车位不存在，请重试");
            return;
        }
        ParkingspotService.deleteInterface(Integer.parseInt(id));
        System.out.println("删除成功");
    }

    private static void addParkingspot() {
        initialize();
        System.out.println("请输入您要添加的停车位的所属停车场id");
        String id=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(id)||!parkinglots.contains(Integer.parseInt(id))){
            System.out.println("输入错误的字符或该停车场不存在，请重试");
            return;
        }
        ParkingspotService.addParkingspot(Integer.parseInt(id));
        System.out.println("添加成功");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private static void addParkinglot() {
        String parkNumber;
        String position;
        System.out.println("请输入您要添加的停车场的停车位数");
        parkNumber=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(parkNumber)){
            return;
        }
        System.out.println("请输入您要添加的停车场的位置");
        position = new Scanner(System.in).next();
        ParkinglotService.addParkinglot(Integer.parseInt(parkNumber),position);

    }

    private static void updateParkinglot() {
        System.out.println("请输入您要修改的停车场id");
        String id=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(id)||!parkinglots.contains(Integer.parseInt(id))){
            System.out.println("输入错误的字符或该停车场不存在，请重试");
            return;
        }
        System.out.println("请输入您想要修改的车位数和位置");
        String parkNumber=new Scanner(System.in).next();;
        if(!numberUtil.isNumeric(parkNumber)){
            System.out.println("请输入正确的数字");
            return;
        }
        String position=new Scanner(System.in).next();
        ParkinglotService.updateParkinglot(Integer.parseInt(parkNumber),position,Integer.parseInt(id));
        System.out.println("已修改成功");
    }




    public static void chargerBookManager() {
        System.out.println("1.增添一个可预约接口记录");
        System.out.println("2.删除一个可预约接口记录");
        System.out.println("3.返回上一级");
        String n=new Scanner(System.in).next();
        switch (n){
            case "1":
                addBook();
                break;
            case "2":
                deleteBook();
                break;
            default:
                System.out.println("输入错误");
                return;
        }
    }

    public static void addBook() {


        System.out.println("请输入您要增添预约时间段的充电口id");
        String id=new Scanner(System.in).next();
        if(!numberUtil.isNumeric(id)||!interfaceids.contains(Integer.parseInt(id))){
            System.out.println("输入错误的字符或该充电口不存在，请重试");
            return;
        }
        Integer chargerid=null;
        List<Interface> interfaces =InterfaceService.getInterfaces();
         for(Interface inter : interfaces){
             if(inter.getId()==Integer.parseInt(id)){
                 chargerid = inter.getChargerid();
             }
        }
        System.out.println("请设置时间段");
       String time=new Scanner(System.in).next();
       CbookService.addBook(id,chargerid,time);
        System.out.println("添加成功");
    }
    public static void deleteBook() {


        System.out.println("请输入您要删除预约时间段的充电口id");
        String id=new Scanner(System.in).next();
        List<Cbooking> cbookings=CbookService.getAll();
        if(!numberUtil.isNumeric(id)){
            System.out.println("输入错误的字符或，请重试");
            return;
        }
        System.out.println("请设置时间段");
        String time=new Scanner(System.in).next();
        for(Cbooking cbooking:cbookings){
            if(cbooking.getId()==Integer.parseInt(id)&&cbooking.getTime().equals(time)){
                CbookService.deleteBook(id,time);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("找不到对应预约记录,请重试");
    }
}
